﻿using Azure.Core;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Materials.API.Application.Command.MaterialRequisitionCommand;
using T.Materials.API.Application.Command.MaterialStatisticsCommand;
using T.Materials.API.MaterialsServer;
using T.Materials.Domain.Model;

namespace T.Materials.API.Controllers
{
    /// <summary>
    /// API控制器--物资统计
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MaterialStatisticsController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public MaterialStatisticsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 新增物资统计
        /// </summary>
        /// <param name="command">新增物资统计命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialStatistics(AddMaterialStatisticsCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "新增物资统计失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除物资统计
        /// </summary>
        /// <param name="command">删除物资统计命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletedMaterialStatistics(DeletedMaterialStatisticsCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
                //result.Code = await mediator.Send(new DeletedMaterialStatisticsCommand
                //{
                //    MaterialStatisticsId=command.MaterialStatisticsId,
                //    DeletedBy= HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除物资统计失败";
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改物资统计
        /// </summary>
        /// <param name="command">修改物资统计命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateMaterialStore(UpdateMaterialStatisticsCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改物资统计失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 批量删除物资统计
        /// </summary>
        /// <param name="command">批量删除物资统计命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDeletedMaterialStatistics(BatchDeletedMaterialStatisticsCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "批量删除物资统计失败";
                throw;
            }
            return result;
        }



    }
}
